package com.lxmes.business.service.impl;

import java.util.List;

import com.lxmes.business.domain.CcRukuInfo;
import com.lxmes.business.mapper.CcRukuInfoMapper;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.NewNumber;
import com.lxmes.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.business.mapper.CcRukuJiesuanMapper;
import com.lxmes.business.domain.CcRukuJiesuan;
import com.lxmes.business.service.ICcRukuJiesuanService;

/**
 * 入库单结算Service业务层处理
 *
 * @author 张志煜
 * @date 2024-07-29
 */
@Service
public class CcRukuJiesuanServiceImpl implements ICcRukuJiesuanService
{
    @Autowired
    private CcRukuJiesuanMapper ccRukuJiesuanMapper;
    @Autowired
    private CcRukuInfoMapper rukuInfoMapper;
    @Autowired
    protected Validator validator;
    /**
     * 查询入库单结算
     *
     * @param id 入库单结算主键
     * @return 入库单结算
     */
    @Override
    public CcRukuJiesuan selectCcRukuJiesuanById(Long id)
    {
        return ccRukuJiesuanMapper.selectCcRukuJiesuanById(id);
    }

    /**
     * 查询入库单结算列表
     *
     * @param ccRukuJiesuan 入库单结算
     * @return 入库单结算
     */
    @Override
    public List<CcRukuJiesuan> selectCcRukuJiesuanList(CcRukuJiesuan ccRukuJiesuan)
    {
        return ccRukuJiesuanMapper.selectCcRukuJiesuanList(ccRukuJiesuan);
    }

    /**
     * 新增入库单结算
     *
     * @param ccRukuJiesuan 入库单结算
     * @return 结果
     */
    @Override
    public int insertCcRukuJiesuan(CcRukuJiesuan ccRukuJiesuan) {
       int count= ccRukuJiesuanMapper.selectCountRukuJiesuanByMonth(ccRukuJiesuan.getJiesuanMonth());
       if(count>0){
            throw new ServiceException(ccRukuJiesuan.getJiesuanMonth()+"该月份已结算！");
       }
        String number= "RKJ"+ NewNumber.createNewNumber(ccRukuJiesuanMapper.getNewCode(DateUtils.getDate()));
        CcRukuInfo rukuInfoParam = new CcRukuInfo();
        rukuInfoParam.setJiesuanMonth(ccRukuJiesuan.getJiesuanMonth());
        rukuInfoParam.setState(1L);
        rukuInfoParam.setFactoryCode(SecurityUtils.getFactoryCode());
        List<CcRukuInfo> rukuInfoList = rukuInfoMapper.selectJieSuanCcRukuInfoList(rukuInfoParam);
        for (CcRukuInfo rukuInfo : rukuInfoList) {
            CcRukuJiesuan jiesuan=new CcRukuJiesuan();
            BeanUtils.copyProperties(rukuInfo,jiesuan);
            jiesuan.setId(null);
            jiesuan.setJiesuanDanhao(number);
            jiesuan.setJiesuanMonth(ccRukuJiesuan.getJiesuanMonth());
            jiesuan.setFactoryCode(SecurityUtils.getFactoryCode());
            jiesuan.setFactoryName(SecurityUtils.getFactoryName());
            jiesuan.setCreateBy(SecurityUtils.getUsername());
            jiesuan.setCreateTime(DateUtils.getNowDate());
            ccRukuJiesuanMapper.insertCcRukuJiesuan(jiesuan);
            rukuInfo.setJiesuanState("1");
            rukuInfo.setJiesuanMonth(ccRukuJiesuan.getJiesuanMonth());
            rukuInfoMapper.updateCcRukuInfo(rukuInfo);
        }
        return  1;
    }
    /**
     * 修改入库单结算
     *
     * @param ccRukuJiesuan 入库单结算
     * @return 结果
     */
    @Override
    public int updateCcRukuJiesuan(CcRukuJiesuan ccRukuJiesuan)
    {
        ccRukuJiesuan.setUpdateTime(DateUtils.getNowDate());
        return ccRukuJiesuanMapper.updateCcRukuJiesuan(ccRukuJiesuan);
    }

    /**
     * 批量删除入库单结算
     *
     * @param ids 需要删除的入库单结算主键
     * @return 结果
     */
    @Override
    public int deleteCcRukuJiesuanByIds(Long[] ids)
    {
        return ccRukuJiesuanMapper.deleteCcRukuJiesuanByIds(ids);
    }

    /**
     * 删除入库单结算信息
     *
     * @param id 入库单结算主键
     * @return 结果
     */
    @Override
    public int deleteCcRukuJiesuanById(Long id)
    {
        return ccRukuJiesuanMapper.deleteCcRukuJiesuanById(id);
    }

    @Override
    public int cancelCcRukuJiesuanByJiesuanMonth(String jiesuanMonth) {
        int count= ccRukuJiesuanMapper.selectCountRukuJiesuanByMonth(jiesuanMonth);
        if(count==0){
            throw new ServiceException(jiesuanMonth+"该月份未结算，无需取消！");
        }
        ccRukuJiesuanMapper.deleteCcRukuJiesuanByJiesuanMonth(jiesuanMonth);
        CcRukuInfo rukuInfoParam = new CcRukuInfo();
        rukuInfoParam.setJiesuanMonth(jiesuanMonth);
        List<CcRukuInfo> rukuInfoList = rukuInfoMapper.selectJieSuanCcRukuInfoList(rukuInfoParam);
        for (CcRukuInfo rukuInfo : rukuInfoList) {
            rukuInfo.setJiesuanState("0");
            rukuInfo.setJiesuanMonth(jiesuanMonth);
            rukuInfoMapper.updateCcRukuInfo(rukuInfo);
        }
        return 1;
    }

    @Override
    public String imporIteminfo(List<CcRukuJiesuan> ccRukuJiesuanList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ccRukuJiesuanList) || ccRukuJiesuanList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (CcRukuJiesuan ccRukuJiesuan : ccRukuJiesuanList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ccRukuJiesuan);
                ccRukuJiesuan.setCreateBy(operName);
                ccRukuJiesuan.setUpdateBy(operName);
                this.insertCcRukuJiesuan(ccRukuJiesuan);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
